var imgSrc;
//点击上传图片
function getToken(){
    var uploadFile = document.getElementById("hide");
    ajax({
        url:pathName+"/api/v1/member/qiniuUploadToken",
        method:"POST",
        dataType:"json",
        success:function(data){
            var obj = JSON.parse(data);
            uploadFile.value = obj.success;
        }
    })
}
function getName() {
    var tijiao = document.querySelector(".denglu");
    //获取选取图片名称
    var file0 = document.querySelector('.file');
    var s = document.querySelector('form').querySelector('s');
    var filed = document.querySelector('.filed');
    var file_head = filed.querySelector(".file_head");
    var file_type = filed.querySelector(".file_type");
    var k = file0.value;
    k = k.split("\\");
    k = k[k.length - 1];
    file_head.style.width = "";
    file_head.innerHTML = k;
    //如果图片名字过长，隐藏部分名字，保留格式
    var width = file_head.clientWidth;
    var parentWidth = filed.clientWidth;
    if(width > parentWidth-100){
        file_head.innerHTML = k.split(".")[0];
        file_head.style.width = "calc(100% - 116px)";
        file_type.innerHTML = "." + k.split(".")[1];

    }else{
        file_type.innerHTML = "";
    }
    //改变右边图标
    s.innerHTML = "替换";
    s.style.cssText = 'background:rgba(0,0,0,0);border:1px solid black;color:black;padding:0 9px;font-size:12px';
    var mask = document.querySelector(".mask");
    mask.style.display = "block";
    //获取图片地址
    //普通上传
    var Qiniu_UploadUrl = "http://up.qiniu.com";
    var Qiniu_upload = function(f, token, key) {
        var xhr = new XMLHttpRequest();
        xhr.open('POST', Qiniu_UploadUrl, true);
        var formData;
        formData = new FormData();
        if (key !== null && key !== undefined) formData.append('key', key);
        formData.append('token', token);
        formData.append('file', f);
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4 && xhr.status == 200 && xhr.responseText != "") {
                var blkRet = JSON.parse(xhr.responseText);
                console && console.log(blkRet);
                console.log(blkRet.hash);
                imgSrc = blkRet.hash;
                imgSrc = "http://img.like-your.com/"+imgSrc;
                var denglu = document.querySelector(".denglu");
                denglu.style.background = "black";
                mask.style.display = "none";
                Tap(tijiao,function(){
                    submit();
                })
            } else if (xhr.status != 200 && xhr.responseText) {

            }
        };
        xhr.send(formData);
    };
    var token = $("#hide").val();
    if ($("#uploadFile")[0].files.length > 0 && token != "") {
        Qiniu_upload($("#uploadFile")[0].files[0], token, $("#key").val());
    } else {
        console && console.log("form input error");
    }
};
//重新提交
function submit(){
    var token = localStorage.getItem("error_token");
    var companyName = document.querySelector(".last");
    var companyAddress = document.querySelector("#adress");
    console.log(imgSrc);
    if($("#uploadFile")[0].files[0]){
        imgSrc += "?imageslim";
        $.ajax({
            url:pathName+"/api/v1/member/updateCompany",
            type:"POST",
            headers:{
                "Accept":"application/json",
                "Authorization":"Bearer "+token
            },
            data:{
                "company_name":companyName.value,
                "company_address":companyAddress.value,
                "company_photo":imgSrc
            },
            success:function(data){
                console.log(data);
                //清除error_token
                localStorage.removeItem("error_token");
                window.location.href = "../../index.html";
            },
            error:function(data){
                console.log(data);
            }
        })
    }
}
window.onload = function(){
    var token = localStorage.getItem("error_token");
    var companyName = document.querySelector(".last");
    var companyAddress = document.querySelector("#adress");
    //ajax请求个人信息
    $.ajax({
        url:pathName+"/api/v1/member",
        type:"get",
        dataType:"json",
        headers:{
            "Accept":"application/json",
            "Authorization":"Bearer "+token
        },
        success:function(data){
            console.log(data.member.company_name);
            companyName.value = data.member.company_name;
            companyAddress.value = data.member.company_address;
            var mask = document.querySelector(".mask");
            mask.style.display = "none";
        }
    });

}